package com.carcontrol.myplugin4.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.carcontrol.myplugin4.R;
import com.carcontrol.myplugin4.core.common.AppManager;
import com.carcontrol.myplugin4.core.widget.LoadingDialog;
import com.carcontrol.myplugin4.utils.LogUtil;
import com.carcontrol.myplugin4.utils.ToastUtil;

import butterknife.BindView;

public abstract class BaseFragment extends Fragment {
	protected View v;


	protected LoadingDialog loadingDialog;
	protected Context context;
	protected Activity activity;

	private String STATE_SAVE_IS_HIDDEN = "dasdasdasd";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		context = getActivity();
		activity = getActivity();

		loadingDialog = new LoadingDialog(context);
		loadingDialog.setCancelable(false);
		loadingDialog.setCanceledOnTouchOutside(false);

		LogUtil.e("bf===onC", savedInstanceState+"==="+isHidden());
	}


	@Override
	public void onHiddenChanged(boolean hidden) {
		super.onHiddenChanged(hidden);

//		LogUtil.e("bf===onHiddenChanged", ((MainActivity)activity).getLastTab()+"==="+((MainActivity)activity).getCurrentTab()+"==="+hidden);
//
//		if(hidden){
//			//pause
//		}else{
//			//resume
//
//			String access_token = SharedPreferencesUrls.getInstance().getString("access_token", "");
//			if("".equals(access_token)){
//				ToastUtil.showMessageApp(context, "请先登录");
//
//				Intent intent = new Intent(BaseApplication.context, LoginActivity.class);
//				startActivity(intent);
//			}else{
//				initHttp();
//			}
//		}
	}


	//如果父、子类在不同的包下，子类要能重写父类的方法，父类方法修饰符必须是protected或public
	protected void initHttp(){};



	@Override
	public void onActivityCreated(@Nullable Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
//		loadingDialog = new LoadingDialog(getActivity());
//		loadingDialog.setMessageText("数据加载...");
//		imageManager = new ImageManager(getActivity());
//		autoHelper = new LibraryHelper(getActivity());

		LogUtil.e("bf===onAC", savedInstanceState+"==="+isHidden());

		context = getActivity();
		BaseApplication.context = context;
	}

	@Override
	public void onSaveInstanceState(@NonNull Bundle outState) {
		LogUtil.e("bf===onSIS", outState+"==="+isHidden());

		outState.putBoolean(STATE_SAVE_IS_HIDDEN, isHidden());
		super.onSaveInstanceState(outState);

	}

	public static byte[] hexStringToByteArray(String str) {
		if(str == null || str.trim().equals("")) {
			return new byte[0];
		}

		byte[] bytes = new byte[str.length() / 2];
		for(int i = 0; i < str.length() / 2; i++) {
			String subStr = str.substring(i * 2, i * 2 + 2);
			bytes[i] = (byte) Integer.parseInt(subStr, 16);
		}

		LogUtil.e("StringToByte===1", bytes+"==="+bytes[0]);


		return bytes;
	}


	public void onStartCommon(final String title) {
		m_myHandler.post(new Runnable() {
			@Override
			public void run() {
				if (loadingDialog != null && !loadingDialog.isShowing()) {
					loadingDialog.setTitle(title);
					loadingDialog.show();
				}
			}
		});

	}


	Handler m_myHandler = new Handler(new Handler.Callback() {
		@Override
		public boolean handleMessage(Message mes) {
			switch (mes.what) {
				case 0:
					break;
				default:
					break;
			}
			return false;
		}
	});

	public void finishMine() {
		AppManager.getAppManager().finishActivity(getActivity());
	}


}
